package com.yc.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 一个Condition可以给多个线程使用，也验证一个Condition就是一个（阻塞）队列
 *	每个线程都可以在里面阻塞
 */
public class TestLockCondition {
	
	Lock lock = new ReentrantLock();
	Condition condition1 = lock.newCondition();
	Condition condition2 = lock.newCondition();
	
	public void m1() {
		Thread t1 = new Thread(() -> {
			try {
				lock.lock();
				System.out.println("--t1-in");
				System.out.println("--t1--condition1.await----");
				condition1.await();
				System.out.println("--t1-out");
			} catch (Exception e) {
			} finally {
				lock.unlock();
			}
		});
		Thread t2 = new Thread(() -> {
			try {
				lock.lock();
				System.out.println("--t2-in");
				System.out.println("--t2--condition1.await----");
				condition1.await();
				System.out.println("--t2-out");
			} catch (Exception e) {
			} finally {
				lock.unlock();
			}
		});
		Thread t3 = new Thread(() -> {
			try {
				lock.lock();
				System.out.println("--t3-in");
				System.out.println("--t3--condition1.signal----");
//				condition1.signal();
//				condition1.signalAll();
				condition2.signalAll();
				System.out.println("--t3-out");
			} catch (Exception e) {
			} finally {
				lock.unlock();
			}
		});
		
		t1.start();
		t2.start();
		t3.start();
		
	}

	public static void main(String[] args) {
		TestLockCondition tc = new TestLockCondition();
		tc.m1();
	}

}
